package com.my.simple.web.controller.admin;

import com.my.simple.common.constant.AuthConstant;
import com.my.simple.entity.auth.Operation;
import com.my.simple.service.auth.OperationService;
import com.my.simple.web.vo.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "operation")
public class OperationController {

    private static final Logger LOGGER = LoggerFactory.getLogger(OperationController.class);
    private static final Integer USERID=10000;
    @Autowired
    private OperationService operationService;

    @PostMapping(value = "addoperation")
    public ResponseEntity<ResponseData> addOperation(@RequestBody Operation operationItem) {
        ResponseData responseData = new ResponseData();
        operationItem.setUpdateBy(USERID);
        operationItem.setUpdateDate(new Date());
        operationService.addOperation(operationItem);
        LOGGER.info("add operation id is {}", operationItem.getOperationId());

        responseData.setMessage("add successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getalloperation")
    public ResponseEntity<ResponseData> getAllOperation() {
        ResponseData responseData = new ResponseData();
        List<Operation> operationList = operationService.getAllOperation();
        responseData.setData(operationList);

        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @GetMapping(value = "getoperationbyid/{operationid}")
    public ResponseEntity<ResponseData> getOperationById(@PathVariable("operationid") int operationId) {
        ResponseData responseData = new ResponseData();
        Operation operation = operationService.getOperationById(operationId);
        responseData.setData(operation);

        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @DeleteMapping(value = "deleteoperationbyid/{operationid}")
    public ResponseEntity<ResponseData> deleteOperationById(@PathVariable("operationid") int operationId) {
        ResponseData responseData = new ResponseData();
        operationService.deleteOperationById(operationId);
        responseData.setMessage("delete successful");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

    @PutMapping(value = "updateoperation")
    public ResponseEntity<ResponseData> updateOperation(@RequestBody Operation operationItem) {
        ResponseData responseData = new ResponseData();
        Integer operationId = operationItem.getOperationId();
        Operation findOperation = operationService.getOperationById(operationId);

        findOperation.setApplicationName(AuthConstant.APPLICATION_NAME);
        findOperation.setOperationName(operationItem.getOperationName());
        findOperation.setOperationType(operationItem.getOperationType());
        findOperation.setOperationDescription(operationItem.getOperationDescription());
        findOperation.setOperationPath(operationItem.getOperationPath());
        findOperation.setParentId(operationItem.getParentId());
        findOperation.setOperationCode(operationItem.getOperationCode());
        findOperation.setNeedPermission(operationItem.getNeedPermission());
        findOperation.setSortNumber(operationItem.getSortNumber());
        findOperation.setActive(operationItem.getActive());
        findOperation.setUpdateDate(new Date());
        findOperation.setUpdateBy(USERID);
        operationService.updateOperation(findOperation);

        responseData.setMessage("update operation success");
        ResponseEntity<ResponseData> responseEntity = new ResponseEntity<ResponseData>(responseData, HttpStatus.OK);
        return responseEntity;
    }

}
